package cn.tutu.blockchain.bank.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.tutu.blockchain.bank.dao.BankDao;
import cn.tutu.blockchain.bank.entity.Bank;
import cn.tutu.blockchain.bank.service.BankService;
import cn.tutu.blockchain.common.Page;
import cn.tutu.blockchain.exception.BusinessException;

@Service
@Transactional
public class BankServiceImpl implements BankService {

	@Autowired
	private BankDao bankDao;

	@Override
	public void create(Bank bank) {
		List<Bank> bankList = bankDao.findByUserId(bank.getUserId());
		if (null != bank && bankList.size() > 0) {
			throw new BusinessException("已存在银行卡信息不能重复添加!");
		}
		bankDao.insert(bank);
	}

	@Override
	public void modify(Bank bank) {
		bankDao.update(bank);
	}

	@Override
	public void remove(String id) {
		bankDao.delete(id);
	}

	@Override
	public PageInfo<Bank> getByUserId(String userId, Page page) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		List<Bank> list = bankDao.findByUserId(userId);
		PageInfo<Bank> pageInfo = new PageInfo<Bank>(list);
		return pageInfo;
	}

}
